package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.RoleInfo;
import com.ldj.hibernate.RoleInfoDAO;
import com.ldj.hibernate.RolePowerInfo;
import com.ldj.hibernate.RolePowerInfoDAO;
import com.ldj.hibernate.RoleUser;
import com.ldj.hibernate.RoleUserDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.RoleInfoForm;

public class RoleInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleInfoDAO dao = new RoleInfoDAO(request, response);
		List<RoleInfo> list= (List<RoleInfo>)dao.findAllPage();
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleInfoDAO dao = new RoleInfoDAO(request, response);
		RoleInfo info = dao.findById(request.getParameter("rid"));
		request.setAttribute("rInfo", info);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleInfoForm dForm = (RoleInfoForm)form;
		RoleInfoDAO dao = new RoleInfoDAO(request, response);
		RoleInfo info = dao.findById(request.getParameter("rid"));
		info.setRoleInfoDescription(dForm.getRoleInfoDescription());
		info.setRoleInfoName(dForm.getRoleInfoName());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "RoleInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getRoleInfoName()));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=list", true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleInfoForm rForm = (RoleInfoForm)form;
		RoleInfo info = new RoleInfo();

		RoleInfoDAO dao = new RoleInfoDAO(request, response);
		info.setRoleInfoDescription(rForm.getRoleInfoDescription());
		info.setRoleInfoName(rForm.getRoleInfoName());
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "RoleInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getRoleInfoName()));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleInfoDAO dao = new RoleInfoDAO(request, response);
		RoleUserDAO rudao = new RoleUserDAO(request, response);
		RolePowerInfoDAO rpdao = new RolePowerInfoDAO(request,response);
		String roleUserRoleId = request.getParameter("rid");
		RoleInfo info = dao.findById(roleUserRoleId);
		dao.delete(info);
		List<RoleUser> infoList = rudao.findByRoleId(roleUserRoleId);
		if(infoList !=null && infoList.size()>0){
			for(int i = 0; i < infoList.size(); i ++){
				rudao.delete(infoList.get(i));
			}
		}
		List<RolePowerInfo> rinfoList = rpdao.findByCode(roleUserRoleId);
		if(rinfoList != null && rinfoList.size()>0){
			for(int i = 0; i < rinfoList.size(); i ++){
				rpdao.delete(rinfoList.get(i));
			}
		}
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "RoleInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getRoleInfoName()));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=list", true);
	}
	
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	//	String code = request.getParameter("code");
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getFunctionMenuTree(request, response, "功能菜单", "FunctionMenu.do?method=list&code=1", "FunctionMenu.do?method=list&code=$code", null));		
		return mapping.findForward("tree");
	}
	
	public ActionForward showUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleUserDAO dao = new RoleUserDAO(request, response);
		PersionFileDAO pDAO = new PersionFileDAO(request, response);
		String roleUserRoleId = request.getParameter("rid");
		
		if(roleUserRoleId !=null && !"".equals(roleUserRoleId)){
			request.getSession().setAttribute("roleUserRoleId", roleUserRoleId);
		}

		roleUserRoleId = (String)request.getSession().getAttribute("roleUserRoleId");
		
		List infoList = dao.findByRoleId(roleUserRoleId);
		if(infoList !=null && infoList.size()>0){
		  String uids = ((RoleUser)infoList.get(0)).getRoleUserUserId();
	      if(uids !=null && !"".equals(uids)){
		    List<PersionFile> pList = pDAO.findByUserIds(uids);
		    request.setAttribute("pList", pList);
	      }
		}
		RoleInfoDAO rDAO=new RoleInfoDAO(request,response);
		String username=rDAO.findById(roleUserRoleId).getRoleInfoName();
		request.setAttribute("username", username);
		return mapping.findForward("showUser");
	}
	
	public ActionForward removeUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleUserDAO dao = new RoleUserDAO(request, response);
		String pid=request.getParameter("pid");
		
		String roleUserRoleId = (String)request.getSession().getAttribute("roleUserRoleId");
		List<RoleUser> infoList = dao.findByRoleId(roleUserRoleId);
		if(infoList !=null && infoList.size()>0){
			RoleUser info1 = infoList.get(0);
			RoleUser info = new RoleUser();
			String uids = info1.getRoleUserUserId();
			if(uids !=null && !"".equals(uids)){
				uids = uids.trim();
			    uids = uids.replaceAll("'"+pid.trim()+"'", "");
			    uids = uids.replaceAll(",,", ",");
			    if(uids.startsWith(",")){
				   uids = uids.substring(1);
			    }
                if(uids.endsWith(",")){
            	   uids = uids.substring(0,uids.length()-1);
		        }
			}else{
				uids = "";
			}    
			info.setRoleUserRoleId(roleUserRoleId);
			info.setRoleUserUserId(uids);

			dao.delete(info1);
			dao.save(info);
		}
		
		return new ActionForward("/RoleInfo.do?method=showUser", true);

	}
	
	public ActionForward addUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleUserDAO dao = new RoleUserDAO(request,response);
		String rid = request.getParameter("rid");
		RoleInfoDAO rDAO=new RoleInfoDAO(request,response);
		String username=rDAO.findById(rid).getRoleInfoName();
		String uid= request.getParameter("uid");
		uid = "'"+uid.replaceAll(",","','")+"'";
		List<RoleUser> infoList = dao.findByRoleId(rid);
		RoleUser info = new RoleUser();
		if(infoList != null && infoList.size()>0){
			RoleUser info1 = infoList.get(0);
			String oldUids = info1.getRoleUserUserId();
			if(oldUids!=null && !"".equals(oldUids)){
			  String[] oldUid = oldUids.split(",");
			  for(int i=0;i<oldUid.length ; i++){
			    if(uid.indexOf(oldUid[i])<0){
				  uid += ","+oldUid[i];
			    }
			  }
			  if(uid.startsWith(",")){
				uid = uid.substring(1);
			  }
			}
			info.setRoleUserRoleId(rid);
			info.setRoleUserUserId(uid);
			dao.delete(info1);
		}else{
			info.setRoleUserRoleId(rid);
			info.setRoleUserUserId(uid);
		}
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("5");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "RoleUserInfo").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", username));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=showUser", true);
	}
}